package com.allwees.bs.module.pms.rest;

import com.allwees.bs.module.core.annotation.PrintLog;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.modelbase.branch.BranchUserContext;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.bs.module.core.dynamic.annotation.DynamicInject;
import com.allwees.bs.module.mms.service.RetailerService;
import com.allwees.bs.module.mms.service.SupplierService;
import com.allwees.bs.module.pms.dto.ProductDto;
import com.allwees.bs.module.pms.entity.Product;
import com.allwees.bs.module.pms.entity.ProductOnoffLog;
import com.allwees.bs.module.pms.req.query.ProductAuditLogQueryReq;
import com.allwees.bs.module.pms.req.query.ProductOnoffLogQueryReq;
import com.allwees.bs.module.pms.req.query.ProductQueryReq;
import com.allwees.bs.module.pms.service.GeneralProductService;
import com.allwees.bs.module.pms.vo.ProductAuditLogVo;
import com.allwees.support.marketing.aop.ResetPrice;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 产品通用功能API
 *
 * @author yangylsky
 * @since 2019-12-12
 *
 */
@PrintLog
@Slf4j
@RestController
@RequestMapping("/backstage/api/pms/product")
public class GeneralProductApi extends BaseProductApi<GeneralProductService> {
	@Autowired
	private RetailerService retailerService;

	@Autowired
	private SupplierService supplierService;
    /**
     * 分页
     *
     * @param req
     * @return
     */
	@DynamicInject(type = DynamicInject.Type.Category,key = "categoryNo")
	@ResetPrice
    @GetMapping("/page")
    public R page(Page<Product> page,  ProductQueryReq req) {
        IPage<ProductDto> retPage = boService.page(page, req);
        addMerchantInfo(retPage);
        return R.ok(retPage);
    }

	private void addMerchantInfo(IPage<ProductDto> page) {
		page.getRecords().forEach(product -> {
			if(StringUtils.isNotBlank(product.getRetailerUuid())) {
				product.setMerchant(retailerService.findByUuid(product.getRetailerUuid()));
			} else if(StringUtils.isNotBlank(product.getSupplierUuid())) {
				product.setMerchant(supplierService.findByUuid(product.getSupplierUuid()));
			}
		});
	}

    /**
	 * 查询产品审核详情
	 *
	 * @param productUuid
	 * @param req
	 * @return
	 */
	@GetMapping("/{productUuid}/audit_log")
	public R auditLog(@PathVariable("productUuid") String productUuid, ProductAuditLogQueryReq req) {
		req.setProductUuid(productUuid);
		List<ProductAuditLogVo> auditLogs = boService.findAuditLog(req);
		return R.ok(auditLogs);
	}

    /**
     * 查询商品上下架日志
     *
     * @param productUuid
	 * @param req
     * @return
     */
    @GetMapping("/{productUuid}/onoff_log")
    public R onoffLog(@PathVariable("productUuid") String productUuid, ProductOnoffLogQueryReq req) {
		req.setProductUuid(productUuid);
		List<ProductOnoffLog> onoffLogs = productOnoffLogService.findOnoffLog(req);
    	return R.ok(onoffLogs);
    }

    /**
     * 查询发布商品汇总数量
     *
     * @return
     */
    @GetMapping("/stat/publish")
    public R statPublish() {
    	return R.ok(boService.queryPublishStat(BranchUserContext.getBranchUuid()));
    }

    /**
     * 查询在售商品汇总数量
     *
     * @return
     */
    @GetMapping("/stat/onsale")
    public R statOnsale() {
    	return R.ok(boService.queryOnSaleStat(BranchUserContext.getBranchUuid()));
    }

    /**
     * 查询下架商品汇总数量
     *
     * @return
     */
    @GetMapping("/stat/putoff")
    public R putoffStat() {
    	return R.ok(boService.queryPutoffStat(BranchUserContext.getBranchUuid()));
    }

	/**
	 * 刷新defno
	 *
	 * @return
	 */
	@GetMapping("/refresh/defno")
	public R refreshDefNo() {
		boService.refreshDefNo();
		return R.ok();
	}
}
